package com.smi.cn.satemachine;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.util.CollectionUtils;

/**
 * Created by zhangyanfei on 2017/7/26.
 *
 * @author zhangyanfei
 * @date 2017/07/26
 */
public class ReturnVo {
    /**
     *
     */
    private Short status;

    public Short getType() {
        return type;
    }

    public void setType(Short type) {
        this.type = type;
    }

    private String msg;
    private Short type;



    public Short getStatus() {
        return status;
    }

    public void setStatus(Short status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public enum RmaStatus {
        PENDING(1, "新申请-待处理", "申请已提交，审核中", true, false),

        CONFIRMING(2, "申请已处理-待确认", "受理中"),

        RETURNING(3, "审核已通过-退货中", "已受理，退货中"),

        RETURNED_CONFIRMING(4, "退货已完成-待确认", "已退货，等待确认收货"),

        RETURNED_RESENDING(5, "退货已确认-发送中", "退货已完成，换货中"),

        REDELIVERED_SENDING(6, "审核已通过-发货中", "已受理，发货中"),

        REFUND_APPROVING(7, "执行已核对-待退款审核", "退货已完成，退款审核中"),

        REDELIVERED_FINISHED(8, "发货已完成-维权结束", "发货已完成，维权结束", false, true),

        REJECTED(9, "审核已驳回-维权结束", "审核已驳回，维权结束", false, true, true),

        REFUND_APPROVED(10, "退款已审核-待退款", "退款已审核，退款中"),

        REFUND_FINISHED(11, "退款已完成-维权结束", "退款已完成，维权结束", false, true),

        CANCELED(12, "用户取消-维权结束", "申请已取消", false, true, true);

        short value;
        String desc;
        String defaultNote;
        boolean isFinal = false;
        boolean initialState = false;
        /**
         * 被终止的流程，CANCEL以及REJECTED都是
         */
        boolean isAbort = false;

        public short value() {
            return value;
        }

        RmaStatus(int value, String desc) {
            this.value = (short)value;
            this.desc = desc;
            this.defaultNote = desc;
        }

        RmaStatus(int value, String desc, String clientDesc) {
            this.value = (short)value;
            this.desc = desc;
            this.defaultNote = clientDesc;
        }

        RmaStatus(int value, String desc, boolean initialState, boolean isFinal) {
            this.value = (short)value;
            this.desc = desc;
            this.defaultNote = desc;
            this.initialState = initialState;
            this.isFinal = isFinal;
        }

        RmaStatus(int value, String desc, String clientDesc, boolean initialState, boolean isFinal) {
            this.value = (short)value;
            this.desc = desc;
            this.defaultNote = clientDesc;
            this.initialState = initialState;
            this.isFinal = isFinal;
        }

        RmaStatus(int value, String desc, String clientDesc, boolean initialState, boolean isFinal, boolean isAbort) {
            this.value = (short)value;
            this.desc = desc;
            this.defaultNote = clientDesc;
            this.initialState = initialState;
            this.isFinal = isFinal;
            this.isAbort = isAbort;
        }

        public static RmaStatus getStatus(int value) {
            RmaStatus[] statuses = RmaStatus.values();
            return Arrays.stream(statuses)
                .filter(status -> status.value() == value)
                .findAny().orElse(null);
        }

        public String getDesc() {
            return desc;
        }

        public boolean isFinal() {
            return isFinal;
        }

        public boolean isInitialState() {
            return initialState;
        }

        public String getDefaultNote() {
            return defaultNote;
        }

        public void setDefaultNote(String defaultNote) {
            this.defaultNote = defaultNote;
        }

        /**
         * 被中断的RMA状态
         *
         * @return
         */
        public static List<RmaStatus> getAbortedRmaStatuses() {
            List<RmaStatus> abortedRmaStatuses = null;
            for (RmaStatus rmaStatus : RmaStatus.values()) {
                if (rmaStatus.isAbort) {
                    if (abortedRmaStatuses == null) {
                        abortedRmaStatuses = new ArrayList<>();
                    }
                    abortedRmaStatuses.add(rmaStatus);
                }
            }
            return abortedRmaStatuses;
        }

        /**
         * 被中断的RMA状态
         *
         * @return
         */
        public static List<Short> getAbortedRmaStatusShortValues() {
            List<RmaStatus> rmaStatuses = getAbortedRmaStatuses();
            if (!CollectionUtils.isEmpty(rmaStatuses)) {
                List<Short> shortValues = new ArrayList<>();
                for (RmaStatus rmaStatus : rmaStatuses) {
                    shortValues.add(rmaStatus.value());
                }
                return shortValues;
            }
            return null;
        }

        @Override
        public String toString() {
            return this.name() + '(' + desc + ')';
        }
    }

    public enum RmaTrigger {
        APPLY_CONFIRM("确认申请"),
        APPLY_APPROVED("通过审核"),
        REJECT("驳回审核"),
        UPLOAD_RETURN_WAYBILL("上传退货单"),
        RETURNED_CONFIRM("收到退货"),
        REFUND_APPROVED("退款审核通过,发起退款"),
        REFUND_FINISH_CONFIRM("完成退款"),
        UPLOAD_REDELIVER_WAYBILL("上传发货单"),
        AUTO_REFUND("自动退款"),
        CANCEL("取消申请");

        private String desc;

        RmaTrigger(String desc) {
            this.desc = desc;
        }

        @Override
        public String toString() {
            return this.name() + '(' + desc + ')';
        }
    }

    public enum RmaType {
        RETURN(1, "T", "退货"),

        EXCHANGE(2, "H", "换货"),

        REDELIVER(3, "B", "补寄"),

        DISCOUNT_REFUND(4, "Z", "折扣退款");

        short value;
        String rmaPrefix;
        String desc;

        RmaType(int value, String rmaPrefix, String desc) {
            this.value = (short)value;
            this.rmaPrefix = rmaPrefix;
            this.desc = desc;
        }

        public short value() { return value; }

        public String getRmaPrefix() {
            return rmaPrefix;
        }

        public static RmaType getType(int value) {
            RmaType[] types = RmaType.values();
            return Arrays.stream(types)
                .filter(type -> type.value() == value)
                .findAny().orElse(null);
        }

        public String getDesc() {
            return desc;
        }

        public boolean isEqual(short type) {
            return this.value == type;
        }

        public boolean isEqual(int type) {
            return this.value == type;
        }
    }
}
